<?php
class module_intall
{
    public $layout = NULL;
    public function __construct()
    {
        $this->view = new system_view();
        $this->model = new model__module_model();
        $this->rt = new handle_route();
        $this->_seokey = $this->rt->rt_get_all_val_seo();
    }
    
    public function index_controller()
    {
        $html = '';
        $_seokey = $this->rt->rt_get_all_val_seo();
        $action = isset($_seokey['2'])?$_seokey['2']:'';
    
        switch($action){
            case '':
                $html = $this->add_module();
            break;
        }
        return $html;
    }
 
    public function add_module(){
        $app = $this->_seokey[0];
        $strHtml = $this->view->view_display('add');
        $error = '';
        $msg = '';
        
        
        if(isset($_POST) && !empty($_POST['module_name'])){
            $module_name = $_POST['module_name'];
            $msgs = $this->cteate_file_module($module_name, $app);
            if(!empty($msgs)){
                foreach($msgs as $msgs){
                    $msg .= $msgs;
                }
            }
            $this->model->md_cm_insert_module($module_name, $app);
            $msg = 'Module đã thêm thành công!';
        }
        
        $strHtml = $this->view->view_assign($strHtml,array('error'=>$error,'msg'=>$msg, 'app'=>$app));
        return $strHtml;
    }
    
    public function cteate_file_module($module_name, $app){
        $_seokey = $this->rt->rt_get_all_val_seo();
        $_message = array();
        $file_tmp = realpath('./temp/module/controller/demo.php');
        $content_source = file_get_contents($file_tmp);
        $content_source = str_replace('demo',$module_name,$content_source);
        /****************create file controller**************/
        $dirModule = realpath('../app/' . $app . '/controller/module');
        $fileModule = $dirModule . '/' . $module_name .'.php';
        
        if(!is_file($fileModule)){
            $fh = fopen($fileModule, 'w+');
            fwrite($fh, $content_source);
            fclose($fh);
        }
        
        
        $list_lang = $this->model->md_cm_get_lang();
        if(!empty($list_lang)){
            foreach($list_lang as $value){
                
                /****************create file lang*******************/
                $dir_lang = realpath('../app/'.$_seokey['0'].'/view/lang/');
                if(!is_dir($dir_lang)){
                    mkdir($dir_lang);
                }
                $dir_lang_cur = $dir_lang . '/' . $value['DirName'];
                if(!is_dir($dir_lang_cur)){
                    mkdir($dir_lang_cur);
                }
                $dir_lang_cur = $dir_lang . '/' . $value['DirName'] . '/' . '/module';
                if(!is_dir($dir_lang_cur)){
                    mkdir($dir_lang_cur);
                }
                
                
                $file_lang_cur = $dir_lang_cur .'/' . $module_name . '.php';
                $file_tmp = realpath('./temp/module/lang/demo.php');
                $content_source = file_get_contents($file_tmp);
                
                $fh = fopen($file_lang_cur, 'w+');
                fwrite($fh, $content_source);
                fclose($fh);
            }
        }
        /****************create file model*******************/
        $dir_model = realpath('../app/'.$_seokey['0'].'/model/module');
        $file_model_cur = $dir_model .'/' . $module_name . '.php';
        $file_tmp = realpath('./temp/module/model/demo.php');
        $content_source = file_get_contents($file_tmp);
        $content_source = str_replace('demo',$module_name,$content_source);
                
        $fh = fopen($file_model_cur, 'w+');
        fwrite($fh, $content_source);
        fclose($fh);
        /**************create file view***********************/
        $dir_view = realpath('../app/'.$_seokey['0'].'/view/temp/default/module');
        $dir_view_cur = $dir_view.'/'.$module_name;
        if(!is_dir($dir_view_cur)){
            mkdir($dir_view_cur);
        }
        $view_array = array('phtml', 'js', 'css');
        foreach($view_array as $file){
            $file_tmp = realpath('./temp/module/view/') . '/index.' . $file;
            $content_source = file_get_contents($file_tmp);
            $content_source = str_replace('demo',$module_name,$content_source);
            $file_view_cur = $dir_view_cur . '/' . $module_name . '.' . $file;
            if(!file_exists($file_view_cur)){
                $fh = fopen($file_view_cur, 'w+');
                fwrite($fh, $content_source);
                fclose($fh);   
            }
        }
        return $_message;
    }
}

